/**
 * ChatON Web API
 * Copyright (c) 2011 Software Center, Samsung Electronics, Inc.
 * All right reserved.
 * This software is the confidential and proprietary information of Samsung
 * Electronics,
 * Inc.
 * ("Confidential Information"). You shall not disclose such Confidential
 * Information and
 * shall use it only in accordance with the terms of the license agreement you
 * entered
 * into with Samsung Electronics.
 */

package com.metel.mservice.common.utils;

import java.io.IOException;
import java.util.HashMap;
import java.util.Properties;

import javax.servlet.http.HttpServletRequest;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;
import org.springframework.core.io.support.PropertiesLoaderUtils;

/**
 * 클래스 설명
 * 
 * @class CommonUtil
 * @see
 * @version 1.0
 * @author iamnow
 */
public class CommonUtil {

	final static Logger logger = LoggerFactory.getLogger(CommonUtil.class);

	public static String getClientIpFromHeader2(HttpServletRequest request) {
		String clientIp = null;

		clientIp = request.getHeader("X-Forwarded-For");
		if (clientIp == null || "".equals(clientIp.trim()) || "null".equals(clientIp)) {
			clientIp = request.getRemoteAddr();
		}

		if (clientIp.indexOf(",") > 0) {
			clientIp = clientIp.substring(0, clientIp.indexOf(",")).trim();
		}

		return clientIp;
	}
	
	public static String getClientIpFromHeader1(HttpServletRequest request) {
		String clientIp = null;

		clientIp = request.getHeader("SERVER-IP");
		return clientIp;
	}
	
	public static String getClientIp(HttpServletRequest request) {
		String clientIp =null;
		
		clientIp = getClientIpFromHeader1(request);
		
		if(clientIp == null || clientIp.equals("")) {
			clientIp = getClientIpFromHeader2(request);
		}
		
		logger.debug("clientip = {}", clientIp);
		
		return clientIp;
	}
	
	public static String getDownloadFileExtension(String propertyVal){
		String extenstion ="";
		Properties p = new Properties();
		Resource resource = new ClassPathResource("downloadfile.extension.properties");
		try {
			p = PropertiesLoaderUtils.loadProperties(resource);
			extenstion = p.getProperty(propertyVal);

		} catch (IOException e) {
			e.printStackTrace();
		}
		
		return extenstion;
	}
	
	public static String getImageStatus(String imageStatus){
		String result ="";
		if(imageStatus!=null && (imageStatus.equals("1") || (imageStatus.equals("4")))){
			result ="1";
			
		}else{
			result ="0";
		}
		return result;
	}
	
	public static String getAppChatonId(HashMap<String,String> buddyMap, String guid){
		String app_chaton_id = null;
		
			if(buddyMap.containsKey(guid)){
				app_chaton_id = buddyMap.get(guid);
			}
		
		return app_chaton_id;
	}
	
}
